package com.fortune.service.impl;

import com.fortune.constants.enums.ProductStock;
import com.fortune.dao.DproductStockDetailInfoDao;
import com.fortune.dao.DstoreInDao;
import com.fortune.dto.in.dStoreIn.*;
import com.fortune.dto.in.dealerWarehouseReceiptWarehouse.ConfirmStorage;
import com.fortune.dto.in.dealerWarehouseReceiptWarehouse.DealerWarehouseReceiptWarehouseDto;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.service.DstoreInService;
import com.fortune.vo.dStorage.DstorageInfo;
import com.fortune.vo.dStore.DstoreInfo;
import com.fortune.vo.dStoreIn.*;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2018/5/26 0026.
 */
@Service
public class DstoreInServiceImpl implements DstoreInService {
    @Autowired
    private DstoreInDao dstoreInDao;
    @Autowired
    private DproductStockDetailInfoDao dproductStockDetailInfoDao;

    @Override
    public List<DstoreInInfo> findStoreList(DstoreInListDto dstoreInListDto, String did) {
        return dstoreInDao.findStoreList(dstoreInListDto,did);
    }

    @Override
    public List<DstoreInfo> findStore(String did) {
        return dstoreInDao.findStore(did);
    }

    @Override
    public List<MData> findMid(String did) {
        return dstoreInDao.findMid(did);
    }

    @Override
    public List<MData> findDidPro(String did) {
        return dstoreInDao.findDidPro(did);
    }

    @Override
    public List<MData> findMidPro(String id) {
        return dstoreInDao.findMidPro(id);
    }

    @Override
    public int findStoreListCount(String did) {
        return dstoreInDao.findStoreListCount(did);
    }

    @Override
    public int queryDInitInfoIsExist(String did, String num, String storeId) {
        return dstoreInDao.queryDInitInfoIsExist(did,num,storeId);
    }

    @Override
    @Transactional
    public int addDInitInfo(String userId,String did,String id,DstoreInitAddDto addDto) {
        int num = dstoreInDao.addDinitInfo(userId,did,id,addDto);
        if (num < 1){
            throw new RuntimeException("添加初始化单失败");
        }
        List<DStoreInitProduceDto> list = addDto.getProduce();
        Integer number = dstoreInDao.findMaxNo(did,id);
        if(number == null){
            number = 1;
        }else{
            number = number+1;
        }
        num = dstoreInDao.addInitDetailInfo(did,id,number,addDto.getProduce());
        if (num < 1){
            throw new RuntimeException("添加初始化单失败");
        }
        return 1;
    }

    @Override
    @Transactional
    public int editDInitInfo(String did,DstoreInitEditDto editDto) {
         dstoreInDao.editDinitInfo(did,editDto);
         dstoreInDao.deleteDinitDetailInfo(did,editDto.getId());
        Integer number = dstoreInDao.findMaxNo(did,editDto.getId());
        if(number == null){
            number = 1;
        }else{
            number = number+1;
        }
        int num = dstoreInDao.editInitDetailInfo(did,editDto.getProduce(),editDto.getId(),number);
        if (num < 1){
            throw new RuntimeException("编辑初始化单失败");
        }
        return 1;
    }

    @Override
    public int deleteDInitInfo(String did, String id) {
        return dstoreInDao.deleteDInitInfo(did,id);
    }

    @Override
    public DinitDetails queryStoreInitDetails(String did, String id) {
        DinitDetails d = dstoreInDao.queryStoreInitDetails(did,id);
        List<DInitDetailsProduce> a = d.getProduce();
        for(DInitDetailsProduce dInitDetailsProduce : a){
            String mid = dInitDetailsProduce.getMid();
            if(mid == null || mid.equals("")){
                dInitDetailsProduce.setMid("");
                dInitDetailsProduce.setmName("自营");
            }
        }
        return d;
    }

    @Override
    @Transactional
    public int storeInitConfirm(String did, String id,String userId) {
        int num = dstoreInDao.updateStoreInit(did,id,userId);
        if (num < 1){
            throw new RuntimeException("确认初始化单失败");
        }
        List<ProductStockDetailInfo> list = dstoreInDao.queryProductStock(did,id);
        for (int i = 0; i < list.size(); i++){
            ProductStockDetailInfo detailInfo = list.get(i);
            int order = dproductStockDetailInfoDao.queryMax(did,detailInfo.getStoreId(),detailInfo.getMpid());
            List<ProductStockDetailInfo> lastDate = dproductStockDetailInfoDao.queryLastStockDetail(did,detailInfo.getStoreId(),detailInfo.getMpid());
            if (!lastDate.isEmpty()){
                detailInfo.setRemainNum(lastDate.get(0).getRemainNum()+detailInfo.getInNum());
            }else {
                detailInfo.setRemainNum(detailInfo.getInNum());
            }
            BigDecimal aa = detailInfo.getPrice();
            num = dproductStockDetailInfoDao.insertSingleInProductStockDetail(detailInfo.getMid(),did,ProductStock.CSHRK,detailInfo,order);
            if (num < 1){
                throw new RuntimeException("确认初始化单失败");
            }
        }
        return 1;
    }

    @Override
    public List<DstorageInfo> findStorageList(DealerWarehouseReceiptWarehouseDto dealerDto, String did) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
       List<DstorageInfo> list = dstoreInDao.findStorageList(dealerDto,did);
       for(DstorageInfo dstorageInfo : list){
           if(dstorageInfo.getInTime() != null){
              dstorageInfo.setInTimes(df.format(dstorageInfo.getInTime()));
           }
           if(dstorageInfo.getCheckTime() != null){
               dstorageInfo.setCheckTimes(df.format(dstorageInfo.getCheckTime()));
           }
       }
        return list;
    }

    @Override
    public int findStorageListCount(DealerWarehouseReceiptWarehouseDto dealerDto, String did) {
        return dstoreInDao.findStorageListCount(dealerDto,did);
    }

    @Override
    @Transactional
    public int confirmStorage(ConfirmStorage confirmStorage, String did,String chickId) {
        String id = confirmStorage.getId();
        int success = dstoreInDao.confireStorage(confirmStorage,did,id,chickId);
        if(success > 0){
            List<StorageDetails> list = dstoreInDao.findStorageDetails(did,id);
            for (int i = 0; i < list.size(); i++){
                StorageDetails storageDetails = list.get(i);
                int order = dproductStockDetailInfoDao.queryMax(did,storageDetails.getStoreId(),storageDetails.getMpid());
                List<ProductStockDetailInfo> lastDate = dproductStockDetailInfoDao.queryLastStockDetail(did,storageDetails.getStoreId(),storageDetails.getMpid());
                if (!lastDate.isEmpty()){
                    storageDetails.setRemainNum(lastDate.get(0).getRemainNum()+storageDetails.getInNum());
                }else {
                    storageDetails.setRemainNum(storageDetails.getInNum());
                }
                success = dproductStockDetailInfoDao.insertStorageProductStockDetail(storageDetails.getMid(),did,storageDetails,order);
                if (success < 1){
                    throw new RuntimeException("确认入库失败");
                }
            }
            return 1;
        }else{
            throw new UpdateInnerErrorException("编辑失败");
        }

    }

    @Override
    public int queryDStoreInIsExist(String num, String did) {
        return dstoreInDao.queryDStoreInIsExist(num,did);
    }

    @Override
    @Transactional
    public int addDStoreIn(String userId, String did, DStoreInAddDto addDto) {
        //入库单ID
        String id = UUID.randomUUID().toString().replace("-","");
        int num = dstoreInDao.addDStoreIn(userId,id,did,addDto);
        if (num < 1){
            throw new RuntimeException("新增失败");
        }
        num = dstoreInDao.addDStoreInInfo(did,id,addDto.getProduce());
        if (num < 1){
            throw new RuntimeException("新增失败");
        }
        return 1;
    }

    @Override
    public int queryEditIsExist(DStoreInEditDto editDto, String did) {
        return dstoreInDao.queryEditIsExist(editDto,did);
    }

    @Override
    @Transactional
    public int editDStoreIn(String userId, String did, DStoreInEditDto editDto) {
        int num = dstoreInDao.editDStoreIn(userId,did,editDto);
        if (num < 1){
            throw new RuntimeException("编辑入库单失败");
        }
        num = dstoreInDao.deleteDStoreInInfo(did,editDto.getId());
        if (num < 1){
            throw new RuntimeException("编辑入库单失败");
        }
        num = dstoreInDao.editDStoreInInfo(did,editDto.getId(),editDto.getProduce());
        if (num < 1){
            throw new RuntimeException("编辑入库单失败");
        }
        return 1;
    }

    @Override
    public int deleteDStoreIn(String did, String id) {
        return dstoreInDao.deleteDStoreIn(did,id);
    }

    @Override
    public List<DStoreInInfoDetail> queryDStoreInInfoDetail(String did, String id) {
        return dstoreInDao.queryDStoreInInfoDetail(did,id);
    }
}
